package com.loyal.loyalerp.humenresource.user.controller;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.loyal.loyalerp.model.GyUser;
import com.loyal.loyalerp.model.vo.EmployeePageInfo;
import com.loyal.loyalerp.model.vo.Result;
import com.loyal.loyalerp.model.vo.UserNameAndCode;
import com.loyal.loyalerp.service.EmployeeService;
import com.loyal.loyalerp.utils.MD5Password;

@Controller
public class EmployController {
	@Autowired
	EmployeeService employService;
	@RequestMapping("getAllEmploy")
	public @ResponseBody EmployeePageInfo getAllEmploy(@RequestParam(name="page",required=false)String page,@RequestParam(name="rows",required=false)String rows,@RequestParam(name="searchOper",required=false)String searchOper,@RequestParam(name="searchField",required=false)String searchField,@RequestParam(name="searchString",required=false)String searchString,@RequestParam(name="_search",required=false)Boolean _search){
		System.out.println(searchString);
		if(_search==false) {
		if(page.equals("1")) {
		PageHelper.startPage(1, Integer.parseInt(rows));	
		List<GyUser> list = employService.getAllEmploy();
		PageInfo<GyUser> info = new PageInfo(list);
		EmployeePageInfo employ = new EmployeePageInfo();
		employ.setRows(list);
	    employ.setPage(info.getPageNum()+"");
	    employ.setTotal(info.getPages()+"");
	    employ.setResult("获取员工");
	  	return employ;
		}
		else {
			PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(rows));	
			List<GyUser> list = employService.getAllEmploy();
			PageInfo<GyUser> info = new PageInfo(list);
			EmployeePageInfo employ = new EmployeePageInfo();
			employ.setRows(list);
		    employ.setPage(info.getPageNum()+"");
		    employ.setTotal(info.getPages()+"");
		    employ.setResult("获取员工");
		  	return employ;
		}
		}
		else {
			if(searchOper!=null) {
				if(searchOper.equals("eq")) {
					if(searchField.equals("usercode")) {
					  List<GyUser> list =	employService.queryUserByUsercode(searchString);
					  EmployeePageInfo employ = new EmployeePageInfo();
					  employ.setRows(list);
					  return employ;
					}
					if(searchField.equals("username")) {
						 List<GyUser> list =	employService.queryUserByUserName(searchString);
						  EmployeePageInfo employ = new EmployeePageInfo();
						  employ.setRows(list);
						  return employ;
					}
				}
			}
		}
		return null;
	}
	
	@ExceptionHandler(com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException.class)
	public  @ResponseBody Result deleteUser() {
		Result result = new Result();
		result.setStatus(500);
		return result;
	}
	
	//增刪改查操作
	@RequestMapping("editEmploy")
	public @ResponseBody Result editUser(@RequestParam(name="companyname",required=false)String companyname,@RequestParam(name="usercode",required=false)String usercode,@RequestParam(name="username",required=false)String username,@RequestParam(name="password",required=false)String password,@RequestParam(name="email",required=false)String email,@RequestParam(name="mobile",required=false)String mobile,@RequestParam(name="weixin",required=false)String weixin,@RequestParam(name="qq",required=false)String qq,@RequestParam(name="remark",required=false)String remark,@RequestParam(name="active",required=false)String active, @RequestParam(name="oper",required=false)String oper,@RequestParam(name="id",required=false)String id,@RequestParam(name="persons",required=false)String persons){
		Result result = new Result();
		if(oper.equals("del")) {
			employService.deleteUser(Integer.parseInt(id));
			result.setStatus(200);
		}
		if(oper.equals("addmore")) {
			MD5Password pass = new MD5Password();
			String newPass = pass.md5(password);
			String [] arr = persons.split(",");
			for(int i=0;i<arr.length;i++) {
				GyUser user1 = employService.selectPersonByCode(arr[i].trim());
				String newPass1 = pass.md5(password);
				GyUser user = new GyUser();
				user.setId(user1.getId());
				user.setCompanyid(user1.getCompanyid());
				user.setUsercode(user1.getUsercode());
				user.setUsername(user1.getUsername());
				user.setPassword(newPass1);
				user.setEmail(user1.getEmail());
				user.setMobile(user1.getMobile());
				user.setWeixin(user1.getWeixin());
				user.setQq(user1.getQq());
				user.setRemark(user1.getRemark());
				user.setCreatetime(new Date());
				user.setActive((byte)Integer.parseInt("1"));
				employService.addUser(user);
				
			}
			result.setStatus(200);
			return result;
		}
		if(oper.equals("add")) {
			//判断用户是否已经存在
			int count = employService.selectUserHasExist(usercode);
			if(count==400) {
				result.setStatus(400);
			}
			else {
			//对密码进行MD5加密
			MD5Password pass = new MD5Password();
			String newPass = pass.md5(password);
			GyUser user = new GyUser();
			int companyid = employService.selectCompanyIdByName(companyname);
			user.setId(Integer.parseInt(id));
			user.setCompanyid(companyid);
			user.setUsercode(usercode);
			user.setUsername(username);
			user.setPassword(newPass);
			user.setEmail(email);
			user.setMobile(mobile);
			user.setWeixin(weixin);
			user.setQq(qq);
			user.setRemark(remark);
			user.setCreatetime(new Date());
			user.setActive((byte)Integer.parseInt(active));
			employService.addUser(user);
			result.setStatus(200);
			}
		}
		if(oper.equals("edit")) {
			String oldusercode = employService.selectUsercodeByPrimaryKey(Integer.parseInt(id));
			if(usercode.equals(oldusercode)) {
				MD5Password pass = new MD5Password();
				String newPass = pass.md5(password);
				GyUser user = new GyUser();
				user.setId(Integer.parseInt(id));
				int companyid = employService.selectCompanyIdByName(companyname);
				user.setCompanyid(companyid);
				user.setUsercode(usercode);
				user.setUsername(username);
				user.setPassword(newPass);
				user.setEmail(email);
				user.setMobile(mobile);
				user.setWeixin(weixin);
				user.setQq(qq);
				user.setRemark(remark);
				user.setUpdatetime(new Date());
				user.setActive((byte)Integer.parseInt(active));
				employService.updateUser(user);
				result.setStatus(200);
			}
			
			else {
				int code = employService.selectUserHasExist(usercode);
				if(code==400) {
					result.setStatus(400);
				}
				else {
					MD5Password pass = new MD5Password();
					String newPass = pass.md5(password);
					GyUser user = new GyUser();
					user.setId(Integer.parseInt(id));
					int companyid = employService.selectCompanyIdByName(companyname);
					user.setCompanyid(companyid);
					user.setUsercode(usercode);
					user.setUsername(username);
					user.setPassword(newPass);
					user.setEmail(email);
					user.setMobile(mobile);
					user.setWeixin(weixin);
					user.setQq(qq);
					user.setRemark(remark);
					user.setUpdatetime(new Date());
					user.setActive((byte)Integer.parseInt(active));
					employService.updateUser(user);
					result.setStatus(200);
				}
			}
		}
		
		return result;
	}
	@RequestMapping("enterUser")
	public String enterUser() {
		return "basedata/user/user";
	}
	
	@RequestMapping("editjsp")
	public String editJsp() {
		return "basedata/user/edit";
	}
	
	@RequestMapping("addjsp")
	public String addJsp() {
		return "basedata/user/add";
	}
	
	@RequestMapping("addmorejsp")
	public String addmoreJsp() {
		return "basedata/user/addmore";
	}
	
	@RequestMapping("company")
	public @ResponseBody String company(){
		List<String> list =  employService.getAllCompany();
		StringBuilder builder = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            builder.append("<option value='" + list.get(i) + "'>" + list.get(i) + "</option>");
        }
		return builder.toString();
	}
	
	
	@RequestMapping("searchjsp")
	public String searchJsp()
	{
		return "basedata/user/search";
	}
	
	//获得所有的用户编码
	@RequestMapping("getAllUserCode")
	public @ResponseBody String getAllUserCode() {
		List<UserNameAndCode> list =  employService.selectAllUserCodeAndName();
		StringBuilder builder = new StringBuilder();
		builder.append("<option></option>");
        for (int i = 0; i < list.size(); i++) {
            builder.append("<option value='" + list.get(i).getPersonCode() + "'>" + list.get(i).getPersonName() +"&nbsp&nbsp&nbsp"+list.get(i).getPersonCode()+ "</option>");
        }
		return builder.toString();
	}
	
	//获得所有的未注册的用户编码
		@RequestMapping("getAllUnRegistPerson")
		public @ResponseBody String getAllUnRegistPerson() {
			List<UserNameAndCode> list =  employService.selectAllUnRegistPerson();
			StringBuilder builder = new StringBuilder();
	        for (int i = 0; i < list.size(); i++) {
	            builder.append("<option value='" + list.get(i).getPersonCode() + "'>" + list.get(i).getPersonName() +"-"+list.get(i).getPersonCode()+ "</option>");
	        }
			return builder.toString();
		}
	
	@RequestMapping("getPersonMsgByUserCode")
	public @ResponseBody GyUser getPersonMsgByUserCode(String usercode) {
		GyUser user = employService.selectPersonByCode(usercode);
		user.setCompanyname(employService.selectCompanyNameById(user.getCompanyid()));
		return user;
	}
}
